<?php
use Base\AdminController;
use Illuminate\Support\Str;

class ConfigController extends AdminController
{
    protected string $table = 'application_configs';

    protected string $model = "applicationConfig";

    #[NoReturn] public function init(): void
    {
        $this->routeWhiteLst = ['config@index','config@add','config@edit','config@del','config@release'];
        parent::init();
        $this->where[] = ['app_id','=',Request::input("app_id")];
    }


    protected function update(): void
    {
        try {
            $params  = Request::all();
            $this->cleanCache();
            $params['create_author'] = $this->user['nickname'];
            if (Request::input("id") !== null) {
                $params['status'] = 0;
                App::make($this->getModelMame())->where("id",Request::input("id"))->update($params);
                $this->apiSuccess([],"更新成功",20000);
            }else {
                $params['uuid'] = (string)Str::uuid();
                $params['created_at'] = date("Y-m-d H:i:s");
                if (DB::schema()->hasColumn($this->table, "org_id")) {
                    $params['org_id'] = $this->org['id'];
                }
                if (DB::schema()->hasColumn($this->table, "user_id")) {
                    $params['user_id'] = $this->member_id;
                }
                $res =  App::make($this->getModelMame())->insertGetId($params);
                $info =  App::make($this->getModelMame())->where('id',$res)->first();
                $this->apiSuccess( $info->toArray(),"新增成功",20000);
            }
        } catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
    }

    public function releaseAction(): void
    {
        $config_dir = ini_get("yaconf.directory");
        $info = ApplicationModel::query()->where("id",Request::input("app_id"))->first();
        $config = ApplicationConfigModel::query()->where("app_id",$info['id'])->get()->toArray();
        $str = '';
        foreach ($config as $vo) {
            if ($vo['remark']) {
                $str.=";".$vo['remark'].PHP_EOL;
            }
            $str.=$vo['name']." = ".$vo['value'].PHP_EOL;
        }
        file_put_contents($config_dir."/".$info['app_name'].".ini", $str);
        if (!Request::input("ids")) {
            $ids = array_column($config,"id");
        }else {
            $ids = Request::input("ids");
        }
        try {
            $value = 1;
            if (is_array($ids)) {
                $res = App::make($this->getModelMame())
                    ->whereIn( App::make($this->getModelMame())->getKeyName(),$ids)
                    ->update(['status'=>$value]);
            } else {
                $res = App::make($this->getModelMame())
                    ->where( App::make($this->getModelMame())->getKeyName(),$ids)
                    ->update(['status'=>$value]);
            }
        }catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
        if (!is_numeric($res)) $this->apiError($res);
        $this->cleanCache();
        $this->apiSuccess([],"发布成功",20000);
    }
}
